package com.chuangjiangx.member.basic.ddd.query;

import com.chuangjiangx.commons.CJBeanUtils;
import com.chuangjiangx.commons.exception.BaseException;
import com.chuangjiangx.domain.shared.model.PayEntry;
import com.chuangjiangx.domain.shared.model.PayType;
import com.chuangjiangx.member.basic.ddd.dal.mapper.MerchantInfoDalMapper;
import com.chuangjiangx.member.basic.ddd.dal.dto.MerchantInfo;
import com.chuangjiangx.member.basic.ddd.dal.dto.MerchantPayConfig;
import com.chuangjiangx.member.basic.ddd.dal.dto.StoreList;
import com.chuangjiangx.member.basic.ddd.query.dto.MerchantInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 查询商户信息
 * Created by YangKuo on 2017/11/22.
 */
@Component
public class MerchantInfoQuery {

    @Autowired
    private MerchantInfoDalMapper merchantInfoDalMapper;

    public MerchantInfoDTO searchMerchantInfoForH5(Long merchantId){
        MerchantInfo info = merchantInfoDalMapper.queryMerchantInfo(merchantId);
        if (info == null) {
            throw new BaseException("","当前商户信息不存在");
        }
        MerchantInfoDTO dto = new MerchantInfoDTO();
        CJBeanUtils.convertBean(info, dto);
        return dto;
    }

    /**
     * 查询是否有此菜单
     * @param merchantComponentId
     */
    public Integer countMerchantComponent(Long merchantComponentId){
        return merchantInfoDalMapper.countMerchantComponent(merchantComponentId);
    }

    /**
     * 查询商户支付配置,有默认值
     * @param payEntry
     * @param payType
     * @param merchantId
     * @return
     */
    public MerchantPayConfig queryPaymentConfig(PayEntry payEntry, PayType payType, Long merchantId) {
        MerchantPayConfig merchantPayConfig = merchantInfoDalMapper.selectPaymentConfig(payEntry.value, payType.value, merchantId);
        if (null == merchantPayConfig) {
            merchantPayConfig = new MerchantPayConfig();
            merchantPayConfig.setPayEntry((byte) payEntry.value);
            merchantPayConfig.setPayType((byte) payType.value);
            merchantPayConfig.setMerchantId(merchantId);
            Integer payChannelId = null;
            switch (payEntry) {
                case WXPAY:
                    payChannelId = 1;
                    break;
                case ALIPAY:
                    payChannelId = 4;
                    break;
                case CASHPAY:
                    payChannelId = 6;
                    break;
                case BESTPAY:
                    payChannelId = 10;
                    break;
                case LBFPAY:
                    payChannelId = 11;
                    break;
                case UNIONPAY:
                    payChannelId = 13;
                    break;
                case MSCARDPAY:
                    payChannelId = 15;
                    break;
                default:
                    throw new BaseException("","the merchant not config payChannelId");
            }
            merchantPayConfig.setPayChannelId(payChannelId);
        }
        return merchantPayConfig;
    }

    public List<StoreList> queryStoreListByMerchantId(Long merchantId){
        return merchantInfoDalMapper.selectStoreListByMerchantId(merchantId);
    }

    public List<StoreList> queryStoreListByEnableAndMerchantId(Long merchantId) {
        return merchantInfoDalMapper.queryStoreListByEnableAndMerchantId(merchantId);
    }
}
